package com.example.market_management.model.vo;

import com.example.market_management.enums.IResponseCode;
import com.example.market_management.enums.ResponseCode;
import lombok.Data;


@Data
public class CommonResult<T> {

    /**
     * 状态码
     */
    private  Integer  code;

    /**
     * 返回信息
     */
    private  String  msg;

    /**
     * 返回数据
     */
    private  T  data;

    protected CommonResult() {
    }

    protected CommonResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     *成功时自定义提示信息和数据
     * @param msg  提示信息
     * @param data 数据
     * @param <T>
     * @return
     */
    public   static  <T> CommonResult<T>  success(String msg, T data){
         return new CommonResult<> (ResponseCode.SUCCESS.getCode(),msg,data);
    }

    /**
     * 成功时自定义返回数据
     * @param data   数据
     * @param <T>
     * @return
     */
    public    static  <T>  CommonResult<T>  success(T data){
        return  new CommonResult<>(ResponseCode.SUCCESS.getCode(),ResponseCode.SUCCESS.getMsg(),data);
    }

    /**
     * 成功时不需要返回数据
     * @param <T>
     * @return
     */

    public   static  <T>  CommonResult<T>   success(){
        return new CommonResult<>(ResponseCode.SUCCESS.getCode(),ResponseCode.SUCCESS.getMsg(),null);
    }

    public  static  <T>  CommonResult<T> success(String  msg){
        return  new CommonResult<>(ResponseCode.SUCCESS.getCode(),msg,null);
    }




    /**
     * 自定义失败时返回信息
     * @param msg
     * @param <T>
     * @return
     */
    public static  <T>   CommonResult<T>  failed(String msg){
        return  new CommonResult<>(ResponseCode.FAILED.getCode(),msg,null);
    }

    /**
     * 失败时返回通用信息
     * @param <T>
     * @return
     */
    public static  <T>  CommonResult<T> failed(Integer code,String  msg){
        return  new CommonResult<>(code,msg,null);
    }

    public   static  <T> CommonResult<T>  failed(String msg, T data){
        return new CommonResult<> (ResponseCode.FAILED.getCode(),msg,data);
    }

}
